package DAO;

import java.net.URLEncoder;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import anroid.vstore.provider.Helper;
import DTO.CategoryDTO;
import DTO.CollectionDTO;
import DTO.ProductDTO;
import DTO.TopicDTO;

public class ProductDAO {
	public static ProductDTO GetProd(int id)
	{
		ProductDTO prod = new ProductDTO();
		String url  = Helper.SERVICE_URL + "api/listAppById.php?id=";
		url+= id+"";
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				
				JSONArray arr = new JSONArray(jsonStr);
				JSONObject c = arr.getJSONObject(0);
				prod.id = c.getInt("Id");
				prod.description = c.getString("description");
				prod.filePath = c.getString("filePath");
				prod.name = c.getString("name");
				prod.imgURL = Helper.SERVICE_URL  + c.getString("thumbImg");
				prod.countDownloads = c.getInt("countDownload");
				prod.publisher = c.getString("publisher");
				prod.createdDate = c.getString("createDate");
				prod.version = c.getString("version");
				prod.packageName = c.getString("packageName");
				prod.category = c.getString("category");
				prod.size = c.getString("size");
				
				JSONArray scrArr = new JSONArray(c.getString("screenShots"));
				String []tokens = scrArr.getString(0).split("<1>");
				for(int j = 0; j < tokens.length;j++)
				{
					if(!tokens[j].equals(""))
						prod.screenShots.add(tokens[j]);
				}
			}catch(Exception e ){}
		}
		return prod;
	}
	public static ArrayList<ProductDTO> GetProds(Activity activity, int cat,int type,int topic, int event, int size)
	{
		ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
		String url = Helper.SERVICE_URL + "api/listapp.php?";
		url += "category=" + cat;
		url += "&type="+type;
		url += "&topic="+topic;
		url += "&event="+ event;
		url += "&limit="+size;
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					ProductDTO prod = new ProductDTO();
					prod.id = c.getInt("Id");
					prod.description = c.getString("description");
					prod.filePath = c.getString("filePath");
					prod.name = c.getString("name");
					prod.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					prod.countDownloads = c.getInt("countDownload");
					prod.publisher = c.getString("publisher");
					prod.createdDate = c.getString("createDate");
					prod.version = c.getString("version");
					prod.packageName = c.getString("packageName");
					prod.category = c.getString("category");
					prod.size = c.getString("size");
					
					JSONArray scrArr = new JSONArray(c.getString("screenShots"));
					String []tokens = scrArr.getString(0).split("<1>");
					for(int j = 0; j < tokens.length;j++)
					{
						if(!tokens[j].equals(""))
							prod.screenShots.add(tokens[j]);
					}
					
					
//					ProductDTO.VerifyAppStatus(activity, prod); // update app status
					list.add(prod);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<ProductDTO> SearchProds(String name, int size)
	{
		ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
		String url = Helper.SERVICE_URL + "api/search.php?";
		url += "name=" + URLEncoder.encode(name);
		url += "&limit="+size;
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					ProductDTO prod = new ProductDTO();
					prod.id = c.getInt("Id");
					prod.description = c.getString("description");
					prod.filePath = c.getString("filePath");
					prod.name = c.getString("name");
					prod.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					prod.countDownloads = c.getInt("countDownload");
					prod.publisher = c.getString("publisher");
					prod.createdDate = c.getString("createDate");
					prod.version = c.getString("version");
					prod.packageName = c.getString("packageName");
					prod.category = c.getString("category");
					prod.size = c.getString("size");
					
					JSONArray scrArr = new JSONArray(c.getString("screenShots"));
					String []tokens = scrArr.getString(0).split("<1>");
					for(int j = 0; j < tokens.length;j++)
					{
						if(!tokens[j].equals(""))
							prod.screenShots.add(tokens[j]);
					}
					list.add(prod);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<CategoryDTO> GetCats(int type)
	{
		ArrayList<CategoryDTO> list = new ArrayList<CategoryDTO>();
		String url = Helper.SERVICE_URL + "api/categoryapp.php";
		if(type == 0)
			url =  Helper.SERVICE_URL + "api/category.php";
		
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					CategoryDTO cat = new CategoryDTO();
					cat.id = c.getInt("Id");
					cat.name = c.getString("Name");
					cat.createdDate = c.getString("createDate");
					cat.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					list.add(cat);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<CollectionDTO> GetCollections()
	{
		ArrayList<CollectionDTO> list = new ArrayList<CollectionDTO>();
		String url = Helper.SERVICE_URL + "api/collection.php";
		
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					CollectionDTO cat = new CollectionDTO();
					cat.id = c.getInt("Id");
					cat.name = c.getString("Name");
					cat.createdDate = c.getString("createDate");
					cat.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					list.add(cat);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<ProductDTO> GetCollectionProds(Activity activity, String collectionId, Integer limit)
	{
		ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
		String url = Helper.SERVICE_URL + "api/listappbycollection.php?";
		url += "id=" + collectionId;
		url += "&limit="+limit.toString();
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					ProductDTO prod = new ProductDTO();
					prod.id = c.getInt("Id");
					prod.description = c.getString("description");
					prod.filePath = c.getString("filePath");
					prod.name = c.getString("name");
					prod.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					prod.countDownloads = c.getInt("countDownload");
					prod.publisher = c.getString("publisher");
					prod.createdDate = c.getString("createDate");
					prod.version = c.getString("version");
					prod.packageName = c.getString("packageName");
					prod.category = c.getString("category");
					prod.size = c.getString("size");
					
					JSONArray scrArr = new JSONArray(c.getString("screenShots"));
					String []tokens = scrArr.getString(0).split("<1>");
					for(int j = 0; j < tokens.length;j++)
					{
						if(!tokens[j].equals(""))
							prod.screenShots.add(tokens[j]);
					}
					
//					ProductDTO.VerifyAppStatus(activity, prod); // update app status
					list.add(prod);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<ProductDTO> GetSamepublisherProds(Activity activity, String appID, Integer limit)
	{
		ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
		String url = Helper.SERVICE_URL + "api/listappbypublisher.php?";
		url += "appId=" + appID;
		url += "&limit="+limit.toString();
		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					ProductDTO prod = new ProductDTO();
					prod.id = c.getInt("Id");
					prod.description = c.getString("description");
					prod.filePath = c.getString("filePath");
					prod.name = c.getString("name");
					prod.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					prod.countDownloads = c.getInt("countDownload");
					prod.publisher = c.getString("publisher");
					prod.createdDate = c.getString("createDate");
					prod.version = c.getString("version");
					prod.packageName = c.getString("packageName");
					prod.category = c.getString("category");
					prod.size = c.getString("size");
					
					JSONArray scrArr = new JSONArray(c.getString("screenShots"));
					String []tokens = scrArr.getString(0).split("<1>");
					for(int j = 0; j < tokens.length;j++)
					{
						if(!tokens[j].equals(""))
							prod.screenShots.add(tokens[j]);
					}
					
//					ProductDTO.VerifyAppStatus(activity, prod); // update app status
					list.add(prod);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<ProductDTO> GetSameCatProds(Activity activity,String appID, Integer limit)
	{
		ArrayList<ProductDTO> list = new ArrayList<ProductDTO>();
		String url = Helper.SERVICE_URL + "api/listappbycategory.php?";
		url += "appId=" + appID;
		url += "&limit="+limit.toString();

		
		String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
		if (jsonStr != null) {
			try {
				JSONArray jsonArr = new JSONArray(jsonStr);
				for(int i =0 ; i < jsonArr.length();i++)
				{
					JSONObject c = jsonArr.getJSONObject(i);
					ProductDTO prod = new ProductDTO();
					prod.id = c.getInt("Id");
					prod.description = c.getString("description");
					prod.filePath = c.getString("filePath");
					prod.name = c.getString("name");
					prod.imgURL = Helper.SERVICE_URL + c.getString("thumbImg");
					prod.countDownloads = c.getInt("countDownload");
					prod.publisher = c.getString("publisher");
					prod.createdDate = c.getString("createDate");
					prod.version = c.getString("version");
					prod.packageName = c.getString("packageName");
					prod.category = c.getString("category");
					prod.size = c.getString("size");
					
					JSONArray scrArr = new JSONArray(c.getString("screenShots"));
					String []tokens = scrArr.getString(0).split("<1>");
					for(int j = 0; j < tokens.length;j++)
					{
						if(!tokens[j].equals(""))
							prod.screenShots.add(tokens[j]);
					}
					
//					ProductDTO.VerifyAppStatus(activity, prod); // update app status
					list.add(prod);
				}
				
			}catch(Exception e){
				}
			}
		return list;
	}
	public static ArrayList<TopicDTO> TestGetTopic(int from, int to)
	{
		ArrayList<TopicDTO> testlist = new ArrayList<TopicDTO>();
		for(int i = from;i<=to;i++)
		{
			TopicDTO prod = new TopicDTO();
			prod.id = i;
			prod.name = "topic"+i;
			testlist.add(prod);
		}
		return testlist;
	}
}
